package com.stu.java8.stream;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: MI
 * @Date: 2021/12/4/16:03
 * @Description:
 */
public class WorldCountDemo {
    String str = "hello world hello";

    @Test
    public void wordCount() {
        Map<String, List<String>> collect = Arrays.stream(str.split(" ")).collect(Collectors.groupingBy(String::toString));
        System.out.println(collect);//{world=[world], hello=[hello, hello]}
        Map<String, Long> collect1 = Arrays.stream(str.split(" ")).collect(Collectors.groupingBy(String::toString, Collectors.counting()));
        System.out.println(collect1);
    }


    @Test
    public void charCount() {
        String[] split = str.split("");
        Stream<String> stringStream = Arrays.stream(str.split(" ")).map(item -> item.split("")).flatMap(Arrays::stream);
        Map<String, Long> collect2 = stringStream.collect(Collectors.groupingBy(String::toString, Collectors.counting()));
        System.out.println(collect2);

        Map<String, Long> collect = Arrays.stream(str.split("")).filter(vo -> !vo.equals(" ")).collect(Collectors.groupingBy(String::toString, Collectors.counting()));
        System.out.println(collect);
    }
}
